package magicloan.auctionpocket.view.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.WindowManager;

import com.utils.module.StringUtils;
import com.utils.module.android.DxPdUtil;

/**
 * Created by Administrator on 2017/2/23.
 */
public class MaterialDialog {
    private Context mContext;
    private static MaterialDialog materDialog = new MaterialDialog();
    private MaterialDialog(){};
    private AlertDialog dialog ;
    private AlertDialog.Builder builder ;
    public static MaterialDialog getMaterialDialog(){
        return materDialog;
    }

    public void initDialog(Context context){
        mContext = context;
  /*
  这里使用了 android.support.v7.app.AlertDialog.Builder
  可以直接在头部写 import android.support.v7.app.AlertDialog
  那么下面就可以写成 AlertDialog.Builder
  */
        builder = new AlertDialog.Builder(mContext);
        dialog = builder.create();
    }

    public void showDialog(String title, String message, int layoutRes, final DialogListener listener){

        if ( !StringUtils.isEmpty(title) ){
            dialog.setTitle(title);
        }
        if ( !StringUtils.isEmpty(message) ){
            dialog.setMessage(message);
        }
        if ( layoutRes > 0 ){
            View contentView = View.inflate(mContext, layoutRes,null);
            dialog.setView(contentView);
            listener.handleContentView(contentView);
        }
        dialog.show();
    }



    public void showDialog(int layoutRes, final DialogListener listener){

        if ( layoutRes > 0 ){
            View contentView = View.inflate(mContext, layoutRes,null);
            dialog.setView(contentView);
            listener.handleContentView(contentView);
        }
        dialog.show();
    }

    public void showDialog(View view, final DialogListener listener){

        if ( view != null ){
            dialog.setView(view);
            listener.handleContentView(view);
        }
        dialog.show();
    }

    public void showDialog(int layoutRes,int width,int height,final DialogListener listener){

        if ( layoutRes > 0 ){
            View contentView = View.inflate(mContext, layoutRes,null);
            dialog.setView(contentView);
            listener.handleContentView(contentView);
        }
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = DxPdUtil.Dp2Px(mContext,width);
        params.height = DxPdUtil.Dp2Px(mContext,height);
//                params.height = 200 ;
        dialog.getWindow().setAttributes(params);

    }

    public void showDialog(View view,int width,int height,final DialogListener listener){

        if ( view != null ){
            dialog.setView(view);
            listener.handleContentView(view);
        }
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = DxPdUtil.Dp2Px(mContext,width);
        params.height = DxPdUtil.Dp2Px(mContext,height);
//                params.height = 200 ;
        dialog.getWindow().setAttributes(params);

    }

    public void showDialog(String title, String message, View view, final DialogListener listener){

        if ( !StringUtils.isEmpty(title) ){
            dialog.setTitle(title);
        }
        if ( !StringUtils.isEmpty(message) ){
            dialog.setMessage(message);
        }
            dialog.setView(view);
            listener.handleContentView(view);
//        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                listener.onNegativeButtonClick();
//            }
//        });
//        builder.setPositiveButton("确定",  new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                listener.onPositiveButtonClick();
//            }
//        });
        dialog.show();
    }

    public void showBuildDialog(String title, String message,String negativeString,String positiveString, final DialogListener listener){

        if ( !StringUtils.isEmpty(title) ){
            builder.setTitle(title);
        }
        if ( !StringUtils.isEmpty(message) ){
            builder.setMessage(message);
        }
        builder.setNegativeButton(negativeString, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.onNegativeButtonClick();
            }
        });
        builder.setPositiveButton(positiveString,  new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.onPositiveButtonClick();
            }
        });
        builder.show();
    }

    public void dismiss(){
        if ( dialog != null )
            dialog.dismiss();
    }

    public interface DialogListener{
        public void onNegativeButtonClick();
        public void onPositiveButtonClick();
        public void handleContentView(View contentView);
    }

}
